package com.zhiwei.jenkins.utils;

import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {
    private static final Charset CHARSET = Charset.forName("UTF-8");

    private static final String SIGN_KEY = "g1u2x3u4n5c6h7e8n9g";//禁止修改

    public static String SHA1ToBase64(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            return Base64.encodeBase64String(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String specialMd5(String decript){
        //请谨慎使用该方法，对应php代码为 MD5(MD5('g1u2x3u4n5c6h7e8n9g').substr(MD5('a123456'),5,20));
        StringBuffer sb = new StringBuffer();
        sb.append(md5(SIGN_KEY));
        sb.append(md5(decript).substring(5,25));
        return md5(sb.toString());
    }

    /**
     *
     * @param str
     * @param str
     * @return
     */
    private static String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes(CHARSET));
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            str = buf.toString();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return str;
    }

    public static String md5Digest(String src) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] b = md.digest(src.getBytes("utf-8"));
        return byte2HexStr(b);
    }

    public static String base64Encoder(String src) throws UnsupportedEncodingException
    {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(src.getBytes("utf-8"));
    }

    public static String base64Encoder(byte[] data) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    public static String base64Decoder(String dest)
            throws NoSuchAlgorithmException, IOException
    {
        BASE64Decoder decoder = new BASE64Decoder();
        return new String(decoder.decodeBuffer(dest), "utf-8");
    }

    private static String byte2HexStr(byte[] b)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; ++i) {
            String s = Integer.toHexString(b[i] & 0xFF);
            if (s.length() == 1)
                sb.append("0");

            sb.append(s.toUpperCase());
        }
        return sb.toString();
    }
//
//    public static void main(String[] args) {
//        System.out.println(specialMd5("123456"));
//    }
}
